/**
 * Commonly used functions
 */

function Common() {}

Common.show = function(src, id) {
	if (id == null)
		var mainImg = document.getElementById("mainImg");
	else
		var mainImg = document.getElementById(id);
	mainImg.src = src;
	
	mainImg.style.backgroundColor;
}
Common.inputDefaultColor = null;

Common.inputActive = function(elem) {
	if (Common.inputDefaultColor == null) {
		Common.inputDefaultColor = elem.style.backgroundColor;
	}
	elem.style.backgroundColor = "white";
}
Common.inputInActive = function(elem) {
	elem.style.backgroundColor = Common.inputDefaultColor;
}
Common.makeInt = function(elem) {
	 var value = parseInt(elem.value.replace(/ /g, ""));
	 if (value >= 0 || value < 0) {
	 	elem.value = value;
	 } else {
	 	elem.value = "";
	 }
}
Common.makeFloat = function(elem) {
	var value = parseFloat(elem.value.replace(/ /g, ""));
	if (value >= 0 || value < 0) {
	 	elem.value = value;
	 } else {
	 	elem.value = "";
	 }
}
Common.clearForm = function(form) {
	for (var n = 0; n < form.elements.length; n++) {
		if (form.elements[n].value && form.elements[n].type != 'submit' && form.elements[n].type != 'button')
			form.elements[n].value = ""
	}
}

/**
 * Add an attribute 'attr' to an object 'obj' with value 'value' 
 * 
 * @param obj - Object to set an attribute
 * @param attr - Which attribute should be set
 * @param value - Value of an attribute. I should end with semicolon ';'.
 */
Common.addAttributeAndKeepOld = function(obj, attr, value) {
	if (typeof(obj) == "undefined" || obj == null || !obj.getAttribute || !obj.setAttribute) {
		return;
	}
	var oldAttr = obj.getAttribute(attr);
	if (typeof(oldAttr) != "undefined" && oldAttr != "" && oldAttr != null) {
		obj.setAttribute(attr, value + oldAttr);
	} else {
		obj.setAttribute(attr, value);
	}
}

/**
 * Combine two arrays. In case of failure return null. Arrays should not overlap.
 * 
 * @param {Array} array1
 * @param {Array} array2
 */
Common.combineArrays = function(array1, array2){
	var array = [];
	if (typeof(array1) == "undefined" || typeof(array2) == "undefined") { 
		return null;
	}
	for (var index in array1) {
		array.push(array1[index]);
	}
	for (var index in array2) {
		array.push(array2[index]);
	}

	return array;
}


Common.removeEmptyImgInputNames = function(inputCurrent) {
	console.log("in removeEmptyImgInputNames");
	var template = document.getElementById("_TEMPLATE_ADDIMG_");
	var parent = template.parentNode;
	var inputs = parent.getElementsByTagName("input");
	
	var i = 0;
	for (var index in inputs) {
		var input = inputs[index];
		
		if (typeof(input) != "undefined" && input.type == "file" && input.value == "") {
			var inputParent = input.parentNode.parentNode;
			if (inputParent != template && inputParent != document.getElementById("offer-edit-empty-one-image-input")) {
				parent.removeChild(inputParent);
			}
		}
	}
}
Common.correctImgInputsNames = function() {
	console.log("in correctImgInputsNames");
	con.log("in correctImgInputsNames");
	var template = document.getElementById("_TEMPLATE_ADDIMG_");
	var parent = template.parentNode;
	var inputs = parent.getElementsByTagName("input");
	
	var i = 0;
	for (var index in inputs) {
		var input = inputs[index];
		
		if (typeof(input) != "undefined" && input.type == "file") {
			input.name = "img_" + i.toString();
			console.log("new name: '" + "img_" + i.toString() + "'");
			i++;
		}
	}
}
Common.copyImgInput = function(input){
	console.log("in copyImgInput");
	var inputToDestroyId = document.getElementById("offer-edit-empty-one-image-input");
	inputToDestroyId.removeAttribute("id");
	
	var template = document.getElementById("_TEMPLATE_ADDIMG_");
	var parent = template.parentNode;
	var newImg = template.cloneNode(true);
	var inputList = newImg.getElementsByTagName("input");
	var inputNew = inputList[0];
	newImg.removeAttribute("id");
	newImg.setAttribute("id", "offer-edit-empty-one-image-input")
	newImg.className = "";
	
	inputNew.value = null;
	console.log(inputNew.value);
	inputNew.name = "img_"; // TODO img with a number e.g. img3
	parent.appendChild(newImg);
	Common.removeEmptyImgInputNames(input);
	Common.correctImgInputsNames();
}
Common.conditionallyDeleteImgInput = function(input){
	console.log("in conditionallyDeleteImgInput");
	if (input.value == "") {
		var template = document.getElementById("_TEMPLATE_ADDIMG_");
		if (input.parentNode.parentNode == template) {
			var arrOfInputs = template.parentNode.getElementsByTagName("input");
			for (var index = arrOfInputs.length - 1; index >= 0; --index) {
				var regExp = new RegExp("^img_");
				if (arrOfInputs[index].value == "" && regExp.test(arrOfInputs[index].name)) {
					var child = arrOfInputs[index].parentNode.parentNode;
					if (child == template || arrOfInputs[index] == input) {
						continue;
					}
					var parent = child.parentNode;
					parent.removeChild(child);
				}
			}
		} else {
			var child = input.parentNode.parentNode;
			var parent = child.parentNode;
			parent.removeChild(child);
		}
	}
	Common.removeEmptyImgInputNames();
	Common.correctImgInputsNames();
};

Common.validateOffer = function(form) {
	var errormsg = Errormsg.getErrormsg();
	
	if (form.zip.value < 10000 || form.zip.value > 99999) {
		errormsg.addError("ZIP je ve spatnem formatu", "offer_zip");
	} else {
		errormsg.removeBox("offer_zip");
	}
	
	return errormsg.countOfMsgs() == 0;	
};

Common.refreshPage = function() {
	window.location.reload(true);
};

/**
 * Make an html element invisible and not to take up a space.
 */
Common.makeInvisible = function(elem) {
	elem.style.visibility = "hidden";
	elem.style.position = "absolute";
};

/**
 * Make an html element visible (additionally to Common.makeInvisible)
 */
Common.makeVisible = function(elem) {
	elem.style.visibility = "visible";
	elem.style.position = "static";
};
